#include<iostream>
#include<vector>
using namespace std;
#include<deque>
deque<int> d1;
void deque_push(int d,int x)
{
    if(d==0)
        d1.push_front(x);
    else
        d1.push_back(x);
}
void deque_randomaccess(int p)
{
    cout<<d1[p]<<'\n';
}
void deque_pop(int d)
{
    if(d==0)
        d1.pop_front();
    else
        d1.pop_back();
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m;
        cin>>m;
        if(m==0)
        {
           int d,x;
           cin>>d>>x;
           deque_push(d,x); 
        }
        else if(m==1)
        {
            int p;
            cin>>p;
            deque_randomaccess(p);
        }
        else
        {
            int d;
            cin>>d;
            deque_pop(d);
        }
    }
    return 0;
}